package com.lbj.googleplay.googleplay100.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lbj.googleplay.googleplay100.R;
import com.lbj.googleplay.googleplay100.domain.AppInfo;
import com.lbj.googleplay.googleplay100.http.protocol.HomeProtocol;
import com.lbj.googleplay.googleplay100.ui.activity.HomeDetailActivity;
import com.lbj.googleplay.googleplay100.ui.adapter.MyBaseAdapter;
import com.lbj.googleplay.googleplay100.ui.holder.BaseHolder;
import com.lbj.googleplay.googleplay100.ui.holder.HomeHeaderHolder;
import com.lbj.googleplay.googleplay100.ui.holder.HomeHolder;
import com.lbj.googleplay.googleplay100.ui.view.LoadingPage;
import com.lbj.googleplay.googleplay100.ui.view.MyListView;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

import java.util.ArrayList;

/*
* 主页
* */
public class HomeFragment extends BaseFragment {

    private ArrayList<AppInfo> data;
    private ArrayList<String> mPictureList;

    //如果加载数据成功,就回调此方法,在主线程运行
    @Override
    public View onCreateSuccessView() {

        MyListView view = new MyListView(UIUtils.getContext());
        //给listview添加头布局,展示轮播条
        HomeHeaderHolder holder = new HomeHeaderHolder();
        view.addHeaderView(holder.getmRootView());
        //先添加头布局,再设置Adapter
        view.setAdapter(new HomeAdapter(data));
        if (mPictureList!=null) {
            //设置轮播条数据
            holder.setData(mPictureList);
        }
        //item的点击事件
        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到详情activity
                AppInfo appInfo = data.get(position-1);//减一是为了跳过头布局
                if (appInfo!=null){
                    Intent intent = new Intent(UIUtils.getContext(), HomeDetailActivity.class);
                    intent.putExtra("packageName",appInfo.packageName);
                    startActivity(intent);
                }
            }
        });
        return view;
    }

    //运行在子线程,可以直接执行耗时网络操作
    @Override
    public LoadingPage.ResultState onLoad() {
        //请求网络数据
        HomeProtocol protocol = new HomeProtocol();

        data = protocol.getData(0);//加载第一页数据
        //获取轮播条图片地址数组
        mPictureList = protocol.getPictureList();

        //校验数据并返回
        return check(data);
    }

    class HomeAdapter extends MyBaseAdapter<AppInfo>{

        public HomeAdapter(ArrayList<AppInfo> list) {
            super(list);
        }

        @Override
        public BaseHolder<AppInfo> getHolder(int position) {

            HomeHolder homeHolder = new HomeHolder();

            return homeHolder;
        }
        //此方法在子线程执行
        @Override
        public ArrayList<AppInfo> onLoadMore() {
//            ArrayList<String> moreData = new ArrayList<>();
//            for (int i=0;i<20;i++){
//                moreData.add("测试更多数据"+i);
//            }
//            SystemClock.sleep(2000);
            HomeProtocol protocol = new HomeProtocol();
            //下一页的数据起始位置等于当前集合的大小getListSize
            ArrayList<AppInfo> moreData = protocol.getData(getListSize());

            return moreData;
        }


        //
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            ViewHolder holder;
//            if(convertView==null){
//                convertView = UIUtils.inflate(R.layout.list_item_home);
//                holder = new ViewHolder();
//                holder.tv_content = convertView.findViewById(R.id.tv_content);
//                convertView.setTag(holder);
//            }else {
//
//                 holder = (ViewHolder) convertView.getTag();
//            }
//
//            String content = getItem(position);
//
//            holder.tv_content.setText(content);
//
//            return convertView;
//        }
    }

}
